package com.liuhung.ash.fang.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.log4j.Logger;
import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


import com.liuhung.base.web.BaseAction;
import com.liuhung.base.util.HtmlUtil;
import com.liuhung.ash.fang.entity.Person;
import com.liuhung.ash.fang.page.PersonPage;
import com.liuhung.ash.fang.service.PersonService;
 
/**
 * <b>项目Controller</b>
 * <b>PersonController<br>
 */ 
@Controller
@RequestMapping("/admin/fang/person") 
public class PersonController extends BaseAction{
	
	private final static Logger log= Logger.getLogger(PersonController.class);
	
	// Servrice start
	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private PersonService personService; 
	
	/**
	 * list页面
	 * @param PersonPage page
	 * @param HttpServletRequest request
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/list") 
	public ModelAndView  list(PersonPage page,HttpServletRequest request) throws Exception{
		Map<String,Object>  context = getRootMap();
		return forword("com/liuhung/ash/fang/person/list",context); 
	}
	
	
	/**
	 * 页面数据datatable展示 所需json
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/data")
	public Map<String, Object> datalist(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Integer draw = getIntegerParameter(request,"draw");
		Integer start = getIntegerParameter(request,"start");
		Integer length =  getIntegerParameter(request,"length");
		Integer orderColumn =getIntegerParameter(request,"order[0][column]");
		String orderDir = request.getParameter("order[0][dir]");
		String searchValue = request.getParameter("search[value]");
		PersonPage page = new PersonPage();
		if(start!=null&&length!=null)
		{
			int pageId =start/length+ 1;
			page.setPage(pageId);
			page.setRows(length);
		}
		String orderField ="";
		if(orderColumn!=null)
		{
			switch (orderColumn) {
						case 1:
				orderField = "id";
				break;
						case 2:
				orderField = "username";
				break;
						case 3:
				orderField = "password";
				break;
						case 4:
				orderField = "sex";
				break;
						case 5:
				orderField = "tel";
				break;
						case 6:
				orderField = "email";
				break;
						case 7:
				orderField = "address";
				break;
						case 8:
				orderField = "datecreated";
				break;
						case 9:
				orderField = "lastupdated";
				break;
						case 10:
				orderField = "status";
				break;
						case 11:
				orderField = "deleted";
				break;
						default:
				orderField = "id";
				break;
			}
		}	
		page.setSort(orderField);
		if(orderDir!=null)
			page.setOrder(orderDir.equals("asc")?0:1);
		int num=personService.queryByCount(page);
		page.setNum(num);
		List<Person> dataList = personService.queryByList(page);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("draw", draw);
		map.put("start",start);
		map.put("length", length);
		map.put("data", dataList);
		map.put("recordsTotal",num);//未过滤的数据条数
		map.put("recordsFiltered",num);//过滤的数据条数
		return map;
	}
	
	
	@RequestMapping("/tree")
	public void datalistForTree(PersonPage page,HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		page.setRows(10000);
		List<Person> dataList = personService.queryByList(page);
		HtmlUtil.writerJson(response, dataList);
	}
	
	/**
	 * 添加或修改数据
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/save")
	public void save(Person entity,Integer[] typeIds,HttpServletResponse response) throws Exception{
		
		if(entity.getId()==null||StringUtils.isBlank(entity.getId().toString())){
			personService.add(entity);
		}else{
			personService.update(entity);
		}
		log.debug("Person对象保存成功！");
		sendSuccessMessage(response, "保存成功~");
	}
	
	
	@RequestMapping("/{id}")
	public void getId(@PathVariable String id,HttpServletResponse response) throws Exception{
		Map<String,Object>  context = new HashMap();
		Person entity  = personService.queryById(id);
		if(entity  == null){
			sendFailureMessage(response, "没有找到对应的记录!");
			return;
		}
		context.put(SUCCESS, true);
		context.put("data", entity);
		HtmlUtil.writerJson(response, context);
	}
	
	
	@RequestMapping("/delete")
	public void delete(@RequestParam("id") String ids,
			HttpServletResponse response) throws Exception {
		String[] id = ids.split(",");
		personService.delete((Object)id);
		sendSuccessMessage(response, "删除成功");
		
	}

}
